<template>
<div id="container">
	<h4 class="m-title">正在热映</h4>
		<swiper :options="swiperOption" ref="mySwiper">
			<swiper-slide  v-for="(article,key) in articles" :key="key">
				<router-link :to="`/subject/${article.id}/${article.title}`" class="link">
					<div class="slide-img-box">
					<img v-lazy="article.images.large">
					</div>
					<h6 class="m-name">{{article.title}}</h6>
				</router-link>
			</swiper-slide>
		</swiper>
		<h4 class="m-title">即将上映</h4>
		<swiper :options="swiperOption" ref="mySwiper">
			<swiper-slide  v-for="(article,key) in articles2" :key="key">
				<router-link :to="`/subject/${article.id}/${article.title}`" class="link">
				<div class="slide-img-box">
					<img v-lazy="article.images.large">
				</div>
				<h6 class="m-name">{{article.title}}</h6>
				</router-link>
			</swiper-slide>
		</swiper>
		<h4 class="m-title top250">豆瓣top100</h4>
		<ul class="top" 
		v-infinite-scroll="loadMore" 
		infinite-scroll-disabled="loading" 
		infinite-scroll-immediate-check="false"
		infinite-scroll-distance="10">
			<li v-for="(article,key) in top250" :key="key">
				<router-link :to="`/subject/${article.id}/${article.title}`">
					<div>
						<img v-lazy.container="article.images.large" width="100%">
					</div>
					<p>{{article.title}}</p>
				</router-link>
			</li>
		</ul>
		<div class="spinner">
			<h5 v-if="!spinner">已经到底了！</h5>
			<mt-spinner v-if="spinner" type="triple-bounce" color="#42bd56" :size="50"></mt-spinner>
		</div>
</div>
</template>
<script>

import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
	components: {
		swiper,
		swiperSlide
	},
    data(){
		return{
			swiperOption: {
				// NotNextTick is a component's own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true)
				// notNextTick是一个组件自有属性，如果notNextTick设置为true，组件则不会通过NextTick来实例化swiper，也就意味着你可以在第一时间获取到swiper对象，假如你需要刚加载遍使用获取swiper对象来做什么事，那么这个属性一定要是true
				// notNextTick: false,
				// swiper configs 所有的配置同swiper官方api配置
				// autoplay: 3000,
				slidesPerView: 3.15,
				paginationClickable: true,
				spaceBetween: 10,
				freeMode: true
				// direction : 'vertical',
				// grabCursor : true,
				// setWrapperSize :true,
				// autoHeight: true,
				// pagination : '.swiper-pagination',
				// paginationClickable :true,
				// prevButton:'.swiper-button-prev',
				// nextButton:'.swiper-button-next',
				// scrollbar:'.swiper-scrollbar',
				// mousewheelControl : true,
				// observeParents:true
				// if you need use plugins in the swiper, you can config in here like this
				// 如果自行设计了插件，那么插件的一些配置相关参数，也应该出现在这个对象中，如下debugger
				// debugger: true,
				// swiper callbacks
				// swiper的各种回调函数也可以出现在这个对象中，和swiper官方一样
				// onTransitionStart(swiper){
				// 	console.log(swiper)
				// }
				// more Swiper configs and callbacks...
				// ...
			},
			articles: [],
			articles2: [],
			top250:[],
			loading: false,
			start:0,
			spinner: true
		}
	},
	methods:{
		loadMore(){
			let count = 10
			let start = this.start
			if(start<100){
				console.log('1111111')
				if(!this.loading){
					console.log('22222222')
					this.loading = true
					this.$http.jsonp('https://api.douban.com/v2/movie/top250?count='+count+'&start='+start, {}, {
						headers: {},
						emulateJSON: true
					}).then(function(response) {
					// 这里是处理正确的回调

						let last = response.data.subjects
						for(let i = 0;i < last.length;i++){
							this.top250.push(last[i])
						}
						this.start += count
						this.loading = false
						// this.articles = response.data["subjects"] 也可以

					}, function(response) {
						// 这里是处理错误的回调
						console.log(response)
					});
				}
			}else{
				this.spinner = false
			}
		}
	},
	computed: {
      swiper() {
        return this.$refs.mySwiper.swiper
      }
    },
	created(){
		this.$store.commit('changeTitle',{
			title: 'HUMA 电影',
			state: false,
			footbar:true
		})
	},
	mounted() {
		this.$http.jsonp('https://api.douban.com/v2/movie/in_theaters?count=10', {}, {
			headers: {},
			emulateJSON: true
		}).then(function(response) {
		// 这里是处理正确的回调

			this.articles = response.data.subjects
			
			// this.articles = response.data["subjects"] 也可以

		}, function(response) {
			// 这里是处理错误的回调
			console.log(response)
		});
		this.$http.jsonp('https://api.douban.com/v2/movie/coming_soon?count=10', {}, {
			headers: {},
			emulateJSON: true
		}).then(function(response) {
		// 这里是处理正确的回调

			this.articles2 = response.data.subjects
			
			// this.articles = response.data["subjects"] 也可以

		}, function(response) {
			// 这里是处理错误的回调
			console.log(response)
		});

		// this.$http.jsonp('https://api.douban.com/v2/movie/top250?count=10', {}, {
		// 	headers: {},
		// 	emulateJSON: true
		// }).then(function(response) {
		// // 这里是处理正确的回调

		// 	this.top250 = response.data.subjects
			
		// 	// this.articles = response.data["subjects"] 也可以

		// }, function(response) {
		// 	// 这里是处理错误的回调
		// 	console.log(response)
		// });
  }

}
</script>
<style scoped>
	.swiper-container{
		height: 150px;
	}
	.link{
		display: block;
	}
	.slide-img-box{
		height:125px;
		overflow:hidden;
		background-color: #ddd;
	}
	.slide-img-box img[lazy=loading] {
		width: 30px;
		margin: 47px auto
		}
	.slide-img-box img{
		display: block;
		width:100%
	}
	.m-name{
		width: 100%;
		margin-top:8px;
		color:#333;
		white-space:nowrap;
		overflow:hidden;
		text-overflow:ellipsis;
	}
	.m-title{
		text-align: left;
		color: #42bd56;
		font-weight: 300;
		padding-left: 5px;
		line-height: 40px;
	}
	.top250{
		text-align: center;
		margin: 20px 0 0;
		line-height: 50px;
	}
	.top{
		width: 100%;
		overflow: hidden;
	}
	.top li{
		float: left;
		width: 33.3333%;
		outline: 1px solid #fff;
	}
	.top li a{
		display: block;
	}
	.top li div{
		height: 150px;
		overflow: hidden;
		background-color: #ddd;
	}
	.top li div img[lazy=loading]{
		width: 30px;
		margin: 60px auto
	}
	.top li div img{
		display: block;
	}
	.top li p{
		width: 100%;
		margin-top:5px;
		color:#333;
		white-space:nowrap;
		overflow:hidden;
		text-overflow:ellipsis;
	}
</style>